/**  
* @Title: MtonsBasicBiz.java
* @Package cn.net.mtons.core.weixin
* @Description: TODO
* @author ma jinjian  
* @date 2016年2月3日 下午11:14:41
* @version V1.0  
*/ 
package cn.net.mtons.weixin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.net.mtons.model.entity.form.MtonsCallbackForm;
import cn.net.mtons.weixin.utils.AesException;
import cn.net.mtons.weixin.utils.MtonsUtils;
import cn.net.mtons.weixin.utils.WXBizMsgCrypt;

/**
 * @ClassName: MtonsBasicBiz
 * @Description: 微信基础接口:获取AccessToken及Jsapi_Ticket
 * @author ma jinjian
 * @date 2016年2月3日 下午11:14:41
 *
 */
@Service(value="basicBiz")
public class MtonsBasicBiz {
	
	@Value("${mtons.token}")
	private String v_token;
	
	@Value("${mtons.encodingAesKey}")
	private String v_encodingaeskey;
	
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger("command");
	
	@SuppressWarnings("unused")
	private static Logger netLog = Logger.getLogger("netlog");

	@Value("${mtons.appId}")
	private String appId;
	
	@Value("${mtons.appSecret}")
	private String appSecret;
	
	@Value("${v_net_accessToken}")
	private String v_net_accessToken;
	
	@Value("${v_net_api_ticket}")
	private String v_net_api_ticket;
	
	/**
	* @Title: getAccessToken
	* @Description: 获取accessToken
	* @param @return
	* @return String 
	* @throws
	*/ 
	public String getAccessToken(){
		v_net_accessToken = v_net_accessToken.replace("#(appId)", appId);
		v_net_accessToken = v_net_accessToken.replace("#(appSecret)", appSecret);
		String str = MtonsUtils.sendGet(v_net_accessToken);
		JSONObject json = JSONObject.fromObject(str);
		if (!json.has("errcode")) {
			return json.getString("access_token");
		}
		return null;
	}
	
	/**
	* @Title: getAPITicket
	* @Description: 获取Jsapi_ticket
	* @param @param accessToken
	* @param @return
	* @return String 
	* @throws
	*/ 
	public String getAPITicket(String accessToken){
		v_net_api_ticket = v_net_api_ticket.replace("#(accessToken)", accessToken);
		String str = MtonsUtils.sendGet(v_net_api_ticket);
		JSONObject json = JSONObject.fromObject(str);
		if (json.has("errcode")) {
			return json.getString("ticket");
		}
		return null;
	}
	
	/**
	* @Title: callback
	* @Description: 回调模式
	* @param @param form
	* @param @param response
	* @return void 
	* @throws
	*/ 
	public void callback(MtonsCallbackForm form, HttpServletResponse response){
		try {
			WXBizMsgCrypt crypt = new WXBizMsgCrypt(v_token, v_encodingaeskey, appId);
			PrintWriter out = response.getWriter();
			String signature = form.getSignature();
			String nonce = form.getNonce();
			String echoStr = form.getEchostr();
			String timeStamp = form.getTimestamp();
			echoStr = crypt.VerifyURL(signature, timeStamp, nonce, echoStr);
			out.write(echoStr);
			out.flush();
			out.close();
		} catch (AesException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
